//
//  ZYEmotionPageViewCell.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/7/5.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit

class ZYEmotionPageViewCell: UICollectionViewCell {
    
    //用于给外界赋值  ==========
    var emotions:[ZYEmotionModel]? {
        //用于给外界赋值的时候使用
        didSet {
            
            //
            
            
            //每当外界给赋值的时候,要把当前cell中的20个按钮全部隐藏
            //遍历隐藏按钮
            for button in emotionButtons {
            
                button.hidden = true
            }
            
            //遍历外界传入的一维数组
            for (i, emotion) in emotions!.enumerate() {
                
                
                
                let button = emotionButtons[i]
                
                //显示button
                button.hidden = false
                
                //给button设置emotion属性
                button.emotion = emotion
                
                /*
                    如果type 是0代表是图片
                    如果type是1代表是字符串
                
                */
                
                //emoji表情
                if emotion.isEmoji {
                   // print(emotions?.count)
                    //7.7 
                    let tit = ((emotion.code ?? "") as NSString).emoji()
                    
                    button.setTitle(tit, forState: UIControlState.Normal)
                    
                    //print(tit)
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                    
                } else {
                    
                    let path = emotion.path ?? ""
                    let png = emotion.png ?? ""
                    let name = path + png
                    
                    //print(name)
                    
                //代表图片表情
                  //  button.setImage(UIImage(named: "\(emotion.png ?? "")"), forState: UIControlState.Normal)
                    
                    let image = UIImage(named: name, inBundle: ZYEmotionTools.sharedTools.emotionBundle, compatibleWithTraitCollection: nil)
                    button.setImage(image, forState: UIControlState.Normal)
                    
                    button.setTitle(nil, forState: UIControlState.Normal)
                    
                    
                    
                }
                
            }
            
        }
    }
    
    //定义保存20个button按钮的
    var emotionButtons:[ZYEmotionButton] = [ZYEmotionButton]()
    
    
    //设置indexPath 
    var indexPath:NSIndexPath? {
        
        didSet{
            
            //messgLabel.text = "第\(indexPath?.section ?? 0)组\(indexPath?.item ?? 0)页"
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加删除按钮的点击事件
    @objc private func deleteBtnClick() {
        //print("点击了删除按钮")
        //发布通知
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionDeleteNoti, object: nil)
        
    }
    
    //7.7  表情按钮的点击事假
    @objc private func buttonClick(button: ZYEmotionButton) {
        
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionButtonNoti , object: button.emotion)
        
        
    }
    
    
    
    //MARK: - 设置自定义cell的视图
    private func setupUI() {
    
        //backgroundColor = RandomColor()
        
        print(ZYEmotionTools.sharedTools.emojiEmotions.count)
        
        addChildButtons()
        
        //7.7  添加删除按钮
        contentView.addSubview(deleteButton)
        
        
        //添加到视图
        contentView.addSubview(messgLabel)
        
        //设置约束
        messgLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView)
            
        }
    }
    
    override func layoutSubviews() {
        //记住按钮的宽度和高度
        
        //7.7==========
        
        let w = (SCREENW - 10) / CGFloat(EmotionMaxCol)
        let h = (216 - 37 - 20) / CGFloat(EmotionMaxRow)
        
        //遍历emotionButtons
        for (i,button) in emotionButtons.enumerate() {
            
            let x = i % EmotionMaxCol
            let y = i / EmotionMaxCol
            
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * h, width: w, height: h)
            
        }
        
        //设置删除按钮的frame
        deleteButton.frame = CGRect(x: SCREENW - 5 - w , y: h * 2, width: w, height: h)
        
    }
    
    
    
    
    //创建20个button按钮
    func addChildButtons() {
        for _ in 0..<EmotionMaxCount {
            //创建button
            let button = ZYEmotionButton()
            
            //添加button 按钮按钮点击事件
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            //给button挺安静dijs
            
            button.titleLabel?.font = UIFont.systemFontOfSize(33)
            
            //设置随机的颜色
            button.backgroundColor = self.backgroundColor
            
            //添加到数组里面
            emotionButtons.append(button)
            //添加button
            contentView.addSubview(button)
            
            
        }
    
    }
    
    
    //懒加载控件
    private lazy var messgLabel: UILabel = {
        
        let labe = UILabel(textColor: UIColor.purpleColor(), textFont: 20)
        labe.textAlignment = .Center
        
        return labe
        
    }()
    
    //7.7删除按钮的懒加载
    private lazy var deleteButton: UIButton = {
        let btn = UIButton()
        
        //添加删除按钮的点击事件7.7
        btn.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
    
        return btn
        
    }()
    
}






























